﻿// ===============================================================================
// Copyright (c) 2007-2020 西安交通信息投资营运有限公司 版权所有
// 作者:焦元义
// ===============================================================================

using DevExpress.XtraEditors;
using System.Linq;
using System.Threading.Tasks;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Filter;
using XCI.Sys.Model;
using XCI.Sys.Service;

namespace XCI.Windows
{
    /// <summary>
    /// 编辑字典类型页面,选择上级下拉TreeList
    /// </summary>
    [System.ComponentModel.DesignerCategory("Dic")]
    public class EditParentDicCategoryTreeListLookUpEdit : TreeListLookUpEdit
    {
        protected override void OnCreateControl()
        {
            base.OnCreateControl();
            if (WinFormHelper.IsDesignMode()) return;

            var treelist = this.Properties.TreeList;
            if (treelist == null) return;
            this.Properties.ValueMember = "Id";
            this.Properties.DisplayMember = "Name";
            this.Properties.PopupWidthMode = PopupWidthMode.UseEditorWidth;
            this.ButtonClick += EditParentDicTreeListLookUpEdit_ButtonClick;
            this.EditValueChanged += EditParentDicTreeListLookUpEdit_EditValueChanged;

            treelist.Columns.AddRange(new[] {
                new DevExpress.XtraTreeList.Columns.TreeListColumn
                {
                    Caption = "名称",
                    FieldName = "Name",
                    Name = "treeColName",
                    Visible = true,
                    VisibleIndex = 0
                }});
            treelist.KeyFieldName = "Id";
            treelist.ParentFieldName = "ParentId";
            treelist.OptionsSelection.EnableAppearanceFocusedCell = false;
            treelist.OptionsView.FocusRectStyle = DevExpress.XtraTreeList.DrawFocusRectStyle.None;
            treelist.OptionsView.ShowColumns = false;
            treelist.OptionsView.ShowHorzLines = false;
            treelist.OptionsView.ShowIndicator = false;
            treelist.OptionsView.ShowVertLines = false;
            treelist.OptionsBehavior.AutoPopulateColumns = true;
            treelist.OptionsView.ShowColumns = false;
            treelist.OptionsView.ShowAutoFilterRow = true;
            treelist.OptionsView.ShowFilterPanelMode = DevExpress.XtraTreeList.ShowFilterPanelMode.Never;
            treelist.OptionsMenu.EnableNodeMenu = false;
            treelist.StateImageList = SysDicService.Instance.GetDicCategoryImageCollection();
            treelist.GetStateImage += (o, e) => e.NodeImageIndex = SysDicService.Instance.GetDicNodeImageIndex(e.Node);
        }

        private void EditParentDicTreeListLookUpEdit_EditValueChanged(object sender, System.EventArgs e)
        {
            if (this.Properties.Buttons.Count >= 1)
            {
                this.Properties.Buttons[1].Visible = this.EditValue != null;
            }
        }

        private void EditParentDicTreeListLookUpEdit_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (this.Properties.Buttons.Count >= 1 && e.Button.Index == 1)
            {
                this.EditValue = null;
                WinFormHelper.SetTreeRootValue(this, null);
            }
        }

        public async Task SetDataSource(bool created, SysDicCategory model)
        {
            var list = await SysDicService.Instance.SelectDicCategoryListAsync(SysDicCategoryFilter.Default);
            if (!created)//修改时移除当前编辑的记录以及所有下级
            {
                var childs = TreeOperateHelper.GetChildren(list, model, true);
                list.Delete(p => childs.Any(x => x.Id.Equals(p.Id)));
            }
            this.Properties.TreeList.DataSource = list;
        }
    }
}